﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class OrbitCamera : MonoBehaviour
{

    [SerializeField] private Transform target;
    public float rotSpeed = 1.5f;
    private float _rotY;
    private float _rotX;
    private Vector3 _offset;

	// Use this for initialization
	void Start ()
	{
	    _rotY = transform.eulerAngles.y;
        //存储摄像机和目标之间的初始位置差
	    _offset = target.position - transform.position;

        //隐藏光标
//        Cursor.lockState = CursorLockMode.Locked;
//	    Cursor.visible = false;
	}
	
	// Update is called once per frame
	void Update () {
		
	}

    void LateUpdate()
    {
//        float horInput = Input.GetAxis("Horizontal");
//        if (horInput != 0)
//        {
//            //使用方向键轻微旋转摄像机
//            _rotY += horInput * rotSpeed;
//        }
//        else
//        {
//            //或者使用鼠标快速旋转摄像机
//            _rotY += Input.GetAxis("Mouse X") * rotSpeed * 3;
//        }

        _rotY -= Input.GetAxis("Mouse X") * rotSpeed;
        Quaternion rotation = Quaternion.Euler(_rotX, _rotY, 0);
        //维持一开始的位置偏移
        transform.position = target.position - (rotation * _offset);
        //不管摄像机相对目标于何处，总是面向目标
        transform.LookAt(target);
    }
}
